<template>
	<gui-page :customHeader="true">
		<view slot="gBody" class="gui-margin">
			<view class="all">
				<view class="img gui-flex gui-rows gui-justify-content-center">
					<image src="../../static/menu_imgs/work.png" mode="widthFix"></image>
				</view>
				<text class="gui-flex gui-rows gui-justify-content-center zi">Hi,请登录~</text>

				<!-- #ifdef APP-PLUS -->
				<view class="bottom">
					<text class="tit">授权登陆</text>
					<view class="info gui-flex gui-rows">
						<text class="my-icons">&#xe501;</text>
						获取你的公开信息(昵称、头像)
					</view>
				</view>
				<view @tap="login_app" class="btn">确认登录</view>
				<!-- #endif -->

				<!-- #ifdef MP-WEIXIN -->
				<view class="bottom">
					<text class="tit">网页由该小程序开发,请确认授权一下信息</text>
					<view class="info gui-flex gui-rows">
						<text class="my-icons">&#xe501;</text>
						获取你的公开信息(昵称、头像)
					</view>
				</view>
				<view @tap="login_wx" class="btn">确认登录</view>
				<view @tap="you" class="btn1">游客登陆</view>
				<!-- #endif -->
			</view>
		</view>
	</gui-page>
</template>

<script>
export default {
	data() {
		return {
			
		};
	},
	onLoad(e) {
		
	},
	onShow() {
		const token = uni.getStorageSync('token');
		if (token) {
			uni.common.post(
				uni.common.apiServer + 'index',
				{
					type: 'get_my_check',
				},
				true,
				res => {
					if (res.code == 1) {
						try {
							uni.setStorageSync('token', res.token);
							uni.setStorageSync('uid', res.uid);
							uni.setStorageSync('user_no', res.user_no);
						} catch (e) {
							// error
						}
						uni.switchTab({
							url: '../index/index'
						});
					}
				},
				e => {
					console.log(e);
				}
			);
		}
	},
	methods: {
		you() {
			uni.switchTab({
				url: '../index/index'
			});
		},
		login_app() {
			const yaoqing = uni.getStorageSync('yaoqing');
			let that=this
			uni.showLoading({
				title: '登入中....'
			});
			uni.getProvider({
				service: 'oauth',
				success: function(res) {
					//console.log(res.provider);
					if (~res.provider.indexOf('weixin')) {
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								// console.log(loginRes);
								uni.getUserInfo({
									provider: 'weixin',
									success: function(infoRes) {

										if(infoRes.errMsg=='getUserInfo:ok'){
											uni.common.post(
												uni.common.apiServer + 'index',
												{
													type: 'get_login_wx_app',
													info: JSON.stringify(infoRes.userInfo),
													user_no:yaoqing
												},
												true,
												res => {
													if (res.code == 1) {
														try {
															uni.setStorageSync('token', res.token);
															uni.setStorageSync('uid', res.uid);
															uni.setStorageSync('user_no', res.user_no);
														} catch (e) {
															// error
														}
														uni.hideLoading()
														uni.switchTab({
															url: '../index/index'
														});
													} else {
														uni.hideLoading()
														uni.showToast({
															title: res.message,
															icon: 'none'
														});
													}
													uni.hideLoading()
												},
												e => {
													uni.hideLoading()
													console.log(e);
												}
											);
										}
									},
									fail() {
										uni.hideLoading()
										uni.showToast({
											icon: 'none',
											title: '登陆失败'
										});
									}
								});
							},
							fail() {
								uni.hideLoading()
								uni.showToast({
									icon: 'none',
									title: '登陆失败'
								});
							}
						});
					}
				}
			});
		},
		login_wx() {
			const yaoqing = uni.getStorageSync('yaoqing');
			uni.showLoading({
				title:'登陆中...'
			})
			let that = this;
			uni.getUserProfile({
				desc: '完善信息',
				success: function(infoRes) {
					// console.log(infoRes);
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							uni.common.post(
								uni.common.apiServer + 'index',
								{
									type: 'get_login_wx_user',
									rawData: infoRes.rawData,
									code: loginRes.code,
									user_no:yaoqing
								},
								true,
								res => {
									if (res.code == 1) {
											try {
												uni.setStorageSync('token', res.token);
												uni.setStorageSync('uid', res.uid);
												uni.setStorageSync('user_no', res.user_no);
											} catch (e) {
												// error
											}
											uni.switchTab({
												url: '../index/index'
											});
									} else {
										uni.showToast({
											title: res.message,
											icon: 'none'
										});
									}
									uni.hideLoading()
								},
								e => {
									uni.hideLoading()
									console.log(e);
								}
							);
						},
						fail() {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: '登陆失败'
							});
						}
					});
				},
				fail() {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '登陆失败'
					});
				}
			});
		}
	}
};
</script>

<style>
.all {
	background-color: #ffffff;
	height: 100vh;
}
.img {
	padding-top: 160rpx;
}
.img image {
	width: 300rpx;
}
.bottom {
	margin-top: 20rpx;
	padding: 0 100rpx;
}
.tit {
	line-height: 1.8;
}
.zi {
	font-size: 38rpx;
	padding-top: 40rpx;
	font-weight: bold;
}
.info {
	margin-top: 20rpx;
	font-size: 25rpx;
	color: #999999;
}
.my-icons {
	line-height: 32rpx;
}
.btn {
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	border-radius: 10rpx;
	margin: 50rpx;
	margin-top: 90rpx;
	color: #ffffff;
	background-color: #1aad17;
}
.btn1 {
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	border-radius: 10rpx;
	margin: 50rpx;
	margin-top: 58rpx;
	color: #333333;
	background-color: #c0d1bf;
}
</style>
